package org.fund.common.util;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.fund.common.constant.FundEnum;
import org.fund.common.model.Fund;
import org.fund.common.model.FundValue;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author Moro
 * @Date 2022/3/6 15:57
 * @title FundTest
 */
public class FundTest {
    public static final String allFundUrl = "https://fundapi.eastmoney.com/fundtradenew.aspx?ft=zs&sc=1n&st=desc&pi=1&pn=65536&cp=&ct=&cd=&ms=&fr=&plevel=&fst=&ftype=&fr1=&fl=0&isab=1";
    public static final String allFundUrl1 = "http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=6yzf&st=desc&sd=2021-03-07&ed=2022-03-07&qdii=&tabSubtype=,,,,,&pi=1&pn=100&dx=1&v=0.";
    public static String userAgent =  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36";
    public static final String referer1 = "http://fund.eastmoney.com/data/fundranking.html";


    // 查找某只基金当日估值的referer
    public static final String estimateReferer="http://fund.eastmoney.com";
    // 查找某只基金当日估值的地址
    public static final String estimateUrl = "http://fundgz.1234567.com.cn/js/004400.js?rt=%s";

    public static void main(String[] args)  {
        List<List<String>> fundDataWithFundType = FundUtils.getFundDataWithFundType(FundEnum.ALL);
        for (List<String> list : fundDataWithFundType) {
            System.out.println(list);
        }

//  当日净值估算
//        HttpRequest httpRequest = HttpRequest.get(estimateUrl+System.currentTimeMillis());
//        httpRequest.header("User-Agent",userAgent);
//        httpRequest.header("Referer", estimateReferer);
//        String body = httpRequest.execute().body();


//        List<List<String>> fundDataWithFundType = FundUtils.getFundDataWithFundType(FundEnum.ZHI_SHU);
//        List<Fund> funds = FundUtils.getFunds(fundDataWithFundType);
//        for (Fund fund : funds) {
//            System.out.println(fund);
//        }
//
//        HttpRequest httpRequest = HttpRequest.get(allFundUrl1+System.currentTimeMillis());
////        HttpRequest httpRequest = HttpRequest.get(allFundUrl);
//        httpRequest.header("User-Agent",userAgent);
//        httpRequest.header("Referer", referer);
//        String body = httpRequest.execute().body();
////        System.out.println(body);
//        int i = body.indexOf("[");
////        System.out.println(i);
//        int i1 = body.indexOf("]");
////        System.out.println(i1);
//        String substring = body.substring(i+2, i1-1);
//        System.out.println(substring);
//        String[] split = substring.split("\",\"");
//        System.out.println(Arrays.toString(split));
//        for (String s : split) {
//            System.out.println(s);
//        }
//        String[] split = substring.split("\",\"");
//        for (String s : split) {
//            String[] split1 = s.split("\\|");
//            System.out.println(Arrays.toString(split1));
//        }
//        String code = "166005";
//        Integer pageIndex = 1;
//        Integer pageSize=20;
//        String startTime = "2022-03-04";
//        String endTime = "2022-03-04";
//        String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";
//        long time = System.currentTimeMillis();
//        String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18306596328894644803_1571038362181&" +
//                "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";
//        url = String.format(url,code,pageIndex,pageSize,startTime,endTime,time);
//        System.out.println("url= " + url);
//        System.out.println(url);
//        HttpRequest request = HttpUtil.createGet(url);
//        request.header("Referer", referer);
//        String str = request.execute().body();
//        //获取str的长度
//        System.out.println("str=" + str);
//        int length = str.length();
//        System.out.println("length=" + length);

//
//        InputStream inputStream = httpRequest.execute().bodyStream();
//        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//        String line = br.readLine();
//        StringBuilder sb = new StringBuilder();
//        while (line != null) {
//            sb.append(line + "\n");
//            line = br.readLine();
//        }
//
//        System.out.println(sb);

//        Integer pageIndex = 1;
//
//        Integer pageSize = 20;
//
//
//
//        String code = "166005";
//        List<FundValue> lsjz = FundUtils.lsjz(code, pageSize, startTime, endTime);
//        System.out.println(lsjz);
////        String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";
//        String referer = "http://fundf10.eastmoney.com";
//        long time = System.currentTimeMillis();
//
//        String url = "http://api.fund.eastmoney.com/f10/lsjz?" +"callback=jQuery18309071382116865634_1646556119321"+
//                "&fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";
//
//        url = String.format(url, code, pageIndex, pageSize, startTime, endTime, time);
//
//        System.out.println("url= " + url);
//
//        System.out.println(url);
//        HttpRequest request = HttpUtil.createGet(url);
//        request.header("Referer", referer);
//        request.header("User-Agent",userAgent);
//
//        String str = request.execute().body();//获取str的长度

//        System.out.println("str=" + str);
//        int length = str.length();

//        System.out.println("length=" + length);//indexOf返回某个指定的字符串值在字符串中首次出现的位置

//        int indexStart = str.indexOf("(");
//        int indexLast = str.indexOf("]");
//
//        str = str.substring(indexStart+9,indexLast+1);
//
//        System.out.println(str);//转换为Obj类型
////
//        JSONObject jsonObject = JSON.parseObject(str+"}");
//        JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");
//        List<FundValue> fundValues = JSONObject.parseArray(jsonArray.toJSONString(), FundValue.class);
//        for (FundValue fundValue : fundValues) {
//            System.out.println(fundValue);
//        }
////
//        System.out.println(jsonObject);//获取数组
//
//        JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");//计算数组的长度
//
//        int size = jsonArray.size();
//
//        System.out.println(size);
    }
    public static JSONArray getFundData(){
        return null;
    }


    public static JSONArray testDepartmentList1(String code) {


        return null;
    }

}
